
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

var blueMarker = new google.maps.Marker();
var greenMarker = new google.maps.Marker();
var redMarker = new google.maps.Marker();

function initialize() {
	directionsDisplay = new google.maps.DirectionsRenderer();
	var chicago = new google.maps.LatLng(41.850033, -87.6500523);
	var myOptions = {
		zoom : 7,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		center : chicago
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
	initializeMarkers(map);
}

function calcRoute() {
	var request = {
		origin : "34.023416, -84.61612",
		destination : "34.075376, -84.29409",
		travelMode : google.maps.DirectionsTravelMode.BICYCLING
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		}
	});
}

// ****************************************************************************
// function: setMarkers
// Adds Markers to the map when called
//
// parameter: map = the desired map to be used
// ****************************************************************************
function initializeMarkers(map) {
	// Add markers to the map

	// Marker sizes are expressed as a Size of X,Y
	// where the origin of the image (0,0) is located
	// in the top left of the image.

	// Origins, anchor positions and coordinates of the marker
	// increase in the X direction to the right and in
	// the Y direction down.
	var blueImage = new google.maps.MarkerImage(
			'Images/xSmall/blue_bike_indicator_xSmall.png',
			// This marker is 20 pixels wide by 32 pixels tall.
			new google.maps.Size(29, 40),
			// The origin for this image is 0,0.
			new google.maps.Point(0, 0),
			// The anchor for this image is the base of the flagpole at 0,32.
			new google.maps.Point(0, 32));
	var redImage = new google.maps.MarkerImage(
			'Images/xSmall/red_bike_indicator_xSmall.png',
			new google.maps.Size(29, 40), new google.maps.Point(0, 0),
			new google.maps.Point(0, 32));
	var greenImage = new google.maps.MarkerImage(
			'Images/xSmall/green_bike_indicator_xSmall.png',
			new google.maps.Size(29, 40), new google.maps.Point(0, 0),
			new google.maps.Point(0, 32));
	var shadow = new google.maps.MarkerImage('Images/shadow.png',
	// The shadow image is larger in the horizontal dimension
	// while the position and offset are the same as for the main image.
	new google.maps.Size(40, 53), new google.maps.Point(0, 0),
			new google.maps.Point(0, 32));
	// Shapes define the clickable region of the icon.
	// The type defines an HTML <area> element 'poly' which
	// traces out a polygon as a series of X,Y points. The final
	// coordinate closes the poly by connecting to the first
	// coordinate.
	var shape = {
		coord : [ 1, 1, 1, 20, 18, 20, 18, 1 ],
		type : 'poly'
	};

	// ////////////////////////////////////////////////////////////////////////////
	// Adds Blue, Green, and Red Markers to the map at specified lats and longs
	// ////////////////////////////////////////////////////////////////////////////
	var blueLatLng = new google.maps.LatLng(34.023416, -84.61612);
	setMarker(blueMarker, blueLatLng, map, shadow, blueImage, shape,'Blue Rider', 0);

	var greenLatLng = new google.maps.LatLng(34.025416, -84.61612);
	setMarker(greenMarker, greenLatLng, map, shadow, greenImage, shape,'Green Rider', 1);

	var redLatLng = new google.maps.LatLng(34.027416, -84.61612);
	setMarker(redMarker, redLatLng, map, shadow, redImage, shape, 'Red Rider', 2);

	setTimeout('updateMarkers()', 1000);
	// ///////////////////////////////////////////////////////////////////////////////
}

function setMarker(marker, position, map, shadow, icon, shape, title, zIndex) {
	marker.setPosition(position);
	marker.setMap(map);
	marker.setShadow(shadow);
	marker.setIcon(icon);
	marker.setShape(shape);
	marker.setTitle(title);
	marker.setZIndex(zIndex);
	//marker.setVisible(false);
}

function updateMarkers() {
	// red-------------------------

	var randomlatR = Math.floor(Math.random() * 5);
	var ltR = 0.05196 * (randomlatR / 100.0);
	var newlatR = redMarker.getPosition().lat() + ltR;
	if (newlatR >= 34.075376)
		newlatR = 34.075376;

	var randomlngR = Math.floor(Math.random() * 4);
	var lgR = 0.32203 * (randomlngR / 100.0);
	var newlngR = redMarker.getPosition().lng() + lgR;
	if (newlngR >= -84.29409)
		newlngR = -84.29409;

	redLatLng = new google.maps.LatLng(newlatR, newlngR);
	redMarker.setPosition(redLatLng);

	// blue------------------------

	var randomlatB = Math.floor(Math.random() * 4);
	var ltB = 0.05196 * (randomlatB / 100.0);
	var newlatB = blueMarker.getPosition().lat() + ltB;
	if (newlatB >= 34.075376)
		newlatB = 34.075376;

	var randomlngB = Math.floor(Math.random() * 3);
	var lgB = 0.32203 * (randomlngB / 100.0);
	var newlngB = blueMarker.getPosition().lng() + lgB;
	if (newlngB >= -84.29409)
		newlngB = -84.29409;

	blueLatLng = new google.maps.LatLng(newlatB, newlngB);
	blueMarker.setPosition(blueLatLng);

	// green-----------------------

	var randomlatG = Math.floor(Math.random() * 3);
	var ltG = 0.05196 * (randomlatG / 100.0);
	var newlatG = greenMarker.getPosition().lat() + ltG;
	if (newlatG >= 34.075376)
		newlatG = 34.075376;

	var randomlngG = Math.floor(Math.random() * 2);
	var lgG = 0.32203 * (randomlngG / 100.0);
	var newlngG = greenMarker.getPosition().lng() + lgG;
	if (newlngG >= -84.29409)
		newlngG = -84.29409;

	greenLatLng = new google.maps.LatLng(newlatG, newlngG);
	greenMarker.setPosition(greenLatLng);
	// repeat!-----
	setTimeout('updateMarkers()', 1000);
}
